package com.example.sharesinfo.utils;

/**
 * @author
 * @type
 * @explain 时间转化类
 * @creat time
 */

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @type
 * @explain 时间转化类
 * @creat time
 */
public class TimeUtils {
    /**
     * 获取当前时间
     */
    public static String getNowTime() {
        Date now = new Date();
        return dateFormat(now);
    }

    /**
     * date类型进行格式化输出（返回类型：String）
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }


    /**
     * 将"2015-08-31 21:08:06"型字符串转化为Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date StringToDate(String str) throws ParseException {
        //2020-02-06 10:50:43
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = (Date) formatter.parse(str);
        return date;
    }

    /**
     * 两个日期相减得到的毫秒数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long dateDiff(Date beginDate, Date endDate) {
        long date1ms = beginDate.getTime();
        long date2ms = endDate.getTime();
        return date2ms - date1ms;
    }

    /**
     * 转换毫秒格式 HH:mm:ss
     * 1s = 1000ms
     * 1m = 60s
     * 1h = 60m
     * 1d = 24h
     *
     * @param ms
     */
    public static String formatDuring(long ms) {
        Date date = new Date();
        date.setTime(ms);
        Date past = new Date();
        try {
            past = StringToDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (past.getYear() - 70 > 0) {
            return past.getYear() + "年前";
        }
        if (past.getMonth() > 0) {
            return past.getMonth() + "月前";
        }
        if (past.getDay() - 4 > 0) {
            return past.getDay() + "天前";
        }
        if (past.getHours() - 8 > 0) {
            return past.getHours() + "小时前";
        }
        if (past.getMinutes() > 0) {
            return past.getMinutes() + "分钟前";
        }
        if (past.getSeconds() > 0) {
            return past.getSeconds() + "秒前";
        }

        return ms + "毫秒前";
    }
}

